/* Andreas Baum
 * Client.java
 * Handles the connection of the iTunes proxy to the login server
 * CS 78 - Networks
 */


/* 
 * The notion of "server" in this project can lead to confusion: 
 * Indeed, it is employed with two separate meanings.
 * 1. The central login server
 * 2. The "server" side of the iTunes connection to which another proxy connect
 * , and which allows another user to access the user's iTunes library.
 */

package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import jbpServerConnector.JBPServerConnector;



public class Client {
	
	public static int SERVER_PORT = 4445;
	
	private static String name;
	private static Socket socket;
	private static PrintWriter out;
	private static BufferedReader in;
	private static String host;
	
	// Default Constructor
	public Client() {
		out = null;
		in = null;
	}

	// Main method:
	public static void main(String args[]) throws IOException {
		socket = null;
		
		BufferedReader keyboardIn =
	        new BufferedReader(
	        new InputStreamReader( System.in ) );
		
		System.out.println("\t*** Welcome to Andreas Baum's Simple Java Bonjour Client! ***");
		
		// Start the proxy's server side, so that other proxys can connect to the client:
		JBPServerConnector JBPserver = new JBPServerConnector();
		
		// user enters username:
		System.out.println("Please enter a username:");
		System.out.print("> ");
		// only the first word, put to lowercase, is the username:
		String username = (keyboardIn.readLine()).toLowerCase();
		username = username.trim();
		username = username.split(" ")[0];
		System.out.println("Username set to " + username + ".");

		// user enters login server host address:
		System.out.println("Please enter the address of the login server:");
		System.out.print("> ");
		host = keyboardIn.readLine();
		
		System.out.println("Attempting to connect to " + host + "...");
		
		// Establish the connection to the login server:
		try {
			// send connection request:
			socket = new Socket(host, SERVER_PORT);
			// establish in and out streams
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
		} catch (UnknownHostException e) {
			// If host can't be found:
			System.err.println("ERROR: Don't know about host: " + host + ".");
			System.exit(1);
		} catch (IOException e) {
			// If host doesn't accept connections:
			System.err.println("ERROR: Couldn't get I/O for "
					+ "the connection to: " + host + ".");
			System.exit(1);
		}
		
		// Confirm connection:
		System.out.println("Connected to " + host 
				+ " on port " + SERVER_PORT + ".");
		System.out.println("Authenticating username...");
		
		// send username to the server
		out.println("USR " + username);
		
		// Receive handshake from server (either accepted username
		// or invalid username)
		String response = in.readLine();
		
		// username is ok
		if (response.toUpperCase().startsWith("PRINT"))
			System.out.println(response.substring(6));
		
		// username is already used
		else {
			System.out.println(response.substring(4));
			System.exit(1);
		}
		
		// connection is established, hand it over to a client thread:
		ClientThread clientThread = new ClientThread(socket);
		
		out.close();
		in.close();
		
	}

	
// ------------------------------------------
// Standard functions:
	
/** Returns the name of the client */
	public String getName() {
		return name;
	}

/** Returns the socket*/
	public Socket getSocket() {
		return socket;
	}

/** Standard toString method*/
	public String toString() {
		return name + "\t" + socket;
	}

}